<template>
  <div class="result-container">
    <div class="title-wrap">
      <!--标题-->
      <h2 class="title">{{$route.query.q}}</h2>
      <span class="sub-title">找到{{count}}个结果</span>
    </div>
    <el-tabs v-model="activeIndex">

      <!-- 单曲块 -->
      <el-tab-pane label="歌曲" name="songs">
        <table class="el-table">
          <thead>
            <th></th>
            <th>音乐标题</th>
            <th>歌手</th>
            <th>专辑</th>
            <th>时长</th>
          </thead>
          <tbody>
            <tr v-for="(item,index) in songList" 
            :key="index" class="el-table__row"
            @dblclick="playMusic(item.id)">
              <td>1</td>
              <td>
                <div class="song-wrap"> 
                  <div class="name-wrap">
                    <!-- 歌名 -->
                    <span>{{item.name}}</span>
                    <!-- mv图标 -->
                    <span v-if="item.mvid!=0" class="iconfont icon-mv"></span>
                  </div>
                  <!-- 二级标题 -->
                  <span v-if="item.alias.length!=0" >{{item.alias[0]}}</span>
                </div>
              </td>
              <td>{{item.artists[0].name}}</td>
              <td>{{item.album.name}}</td>
              <td>{{item.duration}}</td>
            </tr>

          </tbody>
        </table>
      </el-tab-pane>

      <!-- 歌单块 -->
      <el-tab-pane label="歌单"  name="lists">
        <div  v-for="(item,index) in playlists" :key="index" class="items">
          <div class="item">
            <div class="img-wrap">

              <div class="num-wrap">
                播放量:
                <span class="num">{{ item.playCount }}</span>
            </div>

              <img src = 'item.coverImgUrl' alt="" />
              <span class="iconfont icon-play"></span>

            </div>
          <p class="name">{{item.name}}</p>
          
          </div>
        </div>

        <!-- mv块 -->
      </el-tab-pane>
      <el-tab-pane label="MV" name="mv">
        <div class="items mv">
          <div class="item">
            <div class="img-wrap">
              <img src="../assets/mvCover.jpg" alt="" />
              <span class="iconfont icon-play"></span>
              <div class="num-wrap">
                <div class="iconfont icon-play"></div>
                <div class="num">9912</div>
              </div>
              <span class="time">02:43</span>
            </div>
            <div class="info-wrap">
              <div class="name">HEYNA</div>
              <div class="singer">余恩</div>
            </div>
          </div>
          <div class="item">
            <div class="img-wrap">
              <img src="../assets/mvCover.jpg" alt="" />
              <span class="iconfont icon-play"></span>
              <div class="num-wrap">
                <div class="iconfont icon-play"></div>
                <div class="num">9912</div>
              </div>
              <span class="time">02:43</span>
            </div>
            <div class="info-wrap">
              <div class="name">HEYNA</div>
              <div class="singer">余恩</div>
            </div>
          </div>
          <div class="item">
            <div class="img-wrap">
              <img src="../assets/mvCover.jpg" alt="" />
              <span class="iconfont icon-play"></span>
              <div class="num-wrap">
                <div class="iconfont icon-play"></div>
                <div class="num">9912</div>
              </div>
              <span class="time">02:43</span>
            </div>
            <div class="info-wrap">
              <div class="name">HEYNA</div>
              <div class="singer">余恩</div>
            </div>
          </div>
          <div class="item">
            <div class="img-wrap">
              <img src="../assets/mvCover.jpg" alt="" />
              <span class="iconfont icon-play"></span>
              <div class="num-wrap">
                <div class="iconfont icon-play"></div>
                <div class="num">9912</div>
              </div>
              <span class="time">02:43</span>
            </div>
            <div class="info-wrap">
              <div class="name">HEYNA</div>
              <div class="singer">余恩</div>
            </div>
          </div>
          <div class="item">
            <div class="img-wrap">
              <img src="../assets/mvCover.jpg" alt="" />
              <span class="iconfont icon-play"></span>
              <div class="num-wrap">
                <div class="iconfont icon-play"></div>
                <div class="num">9912</div>
              </div>
              <span class="time">02:43</span>
            </div>
            <div class="info-wrap">
              <div class="name">HEYNA</div>
              <div class="singer">余恩</div>
            </div>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name: 'result',
  data() {
    return {
      // tab切换时会改变的数据
      activeIndex: 'songs',
      // 保存查询歌曲
      songList:[],
      // 搜索结果总数
      count: 0,
      //保存歌单的字段
      playlists:[]
    };
  },
    created(){
    axios({
      url: ' https://autumnfish.cn/search',
      method: 'get',
      params:{
        keywords:this.$route.query.q,
        type:1,
        //获取的数据量
        limit:10
      }
    }).then(res=>{
      //console.log(res)
      this.songList = res.data.result.songs

    //计算时间
    for(let i = 0;i<this.songList.length;i++){
      let min = parseInt(this.songList[i].duration/1000/60)
      let sec = parseInt(this.songList[i].duration/1000%60)
      if(min<10){
        min = '0'+min
      } 
      if(sec<10){
        sec = '0'+sec
      }
      // console.log(min+":"+sec)
      this.songList[i].duration = min+":"+sec
    }
    // 保存总数
    this.count  = res.data.result.songCount
    })
  },
  watch: {
    activeIndex(){
      //歌曲
      //列表
      //mv
      let type  = 1;
      switch(this.activeIndex){
        case 'songs':
          type = 1;
          break;
        case 'lists':
          type = 1000;
          break;
        case 'mv':
          type = 1004;
          break;

          default:
            break;
      }
    axios({
      url: ' https://autumnfish.cn/search',
      method: 'get',
      params:{
        keywords:this.$route.query.q,
        type,
        //获取的数据量
        limit:10
      }
    }).then(res=>{
      //console.log(res)
      if(type == 1){
        this.songList = res.data.result.songs

    //计算时间
    for(let i = 0;i<this.songList.length;i++){
      let min = parseInt(this.songList[i].duration/1000/60)
      let sec = parseInt(this.songList[i].duration/1000%60)
      if(min<10){
        min = '0'+min
      } 
      if(sec<10){
        sec = '0'+sec
      }
      // console.log(min+":"+sec)
      this.songList[i].duration = min+":"+sec
    }
    // 保存总数
    this.count  = res.data.result.songCount
      }else if(type == 1000){
        //歌单逻辑
        this.playlists = res.data.result.playlists
        //总数
        this.count  = res.data.result.playlistCount
        //处理播放次数
        for(let i = 0;i  <this.playlists.length;i++){
          if(this.playlist[i].playCount>10000){
            this.playlists[i].playCount = this.playlists[i].playCount/10000 + '万'
          }
        }
      }
      
    })
    }
  },//方法
  methods: {
 playMusic(id) {
        // console.log(id)
        axios({
          url: 'https://autumnfish.cn/song/url',
          method: 'get',
          params: {
            id // id:id
          }
        }).then(res => {
          // console.log(res)
          let url = res.data.data[0].url
          // console.log(this.$parent.musicUrl)
          // 设置给父组件的 播放地址
          this.$parent.musicUrl = url
        })
      },
  },

};
</script>

<style >

</style>
